#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;


class Solution{
    public:
    Solution(){
        myMap['I'] = 1;
        myMap['V'] = 5;
        myMap['X'] = 10;
        myMap['L'] = 50;
        myMap['C'] = 100;
        myMap['D'] = 500;
        myMap['M'] = 1000;
    }

    int ans1(string s){
        int length = s.size();
        vector<int> num(length, 0);
        for(int i = 0; i != length; i++){
            char temp = s[i];
            num[i] = myMap[temp];
        }
        int number = 0;
        for(int i = 0; i != length-1; i++){
            if(num[i] < num[i+1]){
                number = number - num[i];
            }else{
                number = number + num[i];
            }
        }
        number += num[length-1];

        return number;  
    }


    void show(vector<int>& num){
        int length = num.size();
        for(int i = 0; i != length; i++){
            cout << num[i] << " " << ends;
        }
        cout << endl;
    }

    private:
    map<char, int> myMap;
    
};

void test1(){
    Solution s;
    string str = "III";
    int ret = s.ans1(str);
    cout << ret << endl;
}

void test2(){
    Solution s;
    string str = "IV";
    int ret = s.ans1(str);
    cout << ret << endl;
}

void test3(){
    Solution s;
    string str = "LVIII";
    int ret = s.ans1(str);
    cout << ret << endl;
}

void test4(){
    Solution s;
    string str = "MCMXCIV";
    int ret = s.ans1(str);
    cout << ret << endl;
}

int main(void){
    test1();
    test2();
    test3();
    test4();

    return 0;
}